package com.gx.cn.hospitalmanage;


import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.widget.RadioButton;
import android.widget.RadioGroup;


import com.gx.cn.hospitalmanage.Fragment.MyFragmentPagerAdapter;


public class MainActivity extends AppCompatActivity

        implements RadioGroup.OnCheckedChangeListener,
        ViewPager.OnPageChangeListener {

    private RadioGroup rg_tab_bar;
    private RadioButton rb_channel;
    private RadioButton rb_message;
    private RadioButton rb_better;
    private RadioButton rb_setting;
    private ViewPager vpager;

 private MyFragmentPagerAdapter mAdapter;


    //几个代表页面的常量
    public static final int PAGE_ONE = 0;
    public static final int PAGE_TWO = 1;
    public static final int PAGE_THREE = 2;
    public static final int PAGE_FOUR = 3;
    Toolbar toolbar;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mAdapter = new MyFragmentPagerAdapter(getSupportFragmentManager());

        bindViews();
        rb_channel.setChecked(true); //导航栏按钮默认打开第一个

        //顶部标题
         toolbar = (Toolbar) findViewById(R.id.toolbar);

        setSupportActionBar(toolbar); //

        //轮播图
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);


    }



    //轮播图加载图片
    @Override
    public void onBackPressed() {
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    }

    //导航栏点击事件
    private void bindViews() {

        rg_tab_bar = (RadioGroup) findViewById(R.id.rg_tab_bar);
        rb_channel = (RadioButton) findViewById(R.id.rb_channel);
        rb_message = (RadioButton) findViewById(R.id.rb_message);
        rb_better = (RadioButton) findViewById(R.id.rb_better);
        rb_setting = (RadioButton) findViewById(R.id.rb_setting);
        rg_tab_bar.setOnCheckedChangeListener(this);


        vpager = (ViewPager) findViewById(R.id.vpager);
        vpager.setAdapter(mAdapter);
        vpager.setCurrentItem(0);
        vpager.addOnPageChangeListener(this);
    }


  //切换Fragment页面
    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {

        switch (checkedId) {
            case R.id.rb_channel:
                vpager.setCurrentItem(PAGE_ONE);

                break;
            case R.id.rb_message:
                vpager.setCurrentItem(PAGE_TWO);

                break;
            case R.id.rb_better:
                vpager.setCurrentItem(PAGE_THREE);

                break;
            case R.id.rb_setting:
                vpager.setCurrentItem(PAGE_FOUR);

                break;
        }

    }


    //重写ViewPager页面切换的处理方法
    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }


    @Override
    public void onPageSelected(int position) {

    }


    @Override
    public void onPageScrollStateChanged(int state) {
        //state的状态有三个，0表示什么都没做，1正在滑动，2滑动完毕
        if (state == 2) {
            switch (vpager.getCurrentItem()) {
                case PAGE_ONE:
                    rb_channel.setChecked(true);
                    toolbar.setTitle("首页");
                    break;
                case PAGE_TWO:
                    rb_message.setChecked(true);
                    toolbar.setTitle("消息");
                    break;
                case PAGE_THREE:
                    rb_better.setChecked(true);
                    toolbar.setTitle("就诊人信息");
                    break;
                case PAGE_FOUR:
                    rb_setting.setChecked(true);
                    toolbar.setTitle("个人中心");
                    break;
            }
        }
    }



}
